let v = new Vue({
    el:'#app',
    data:{
        user:{
            email:'',
            password:''
        },
        code:'',
        msg:''
    },
    methods:{
        //检查邮箱格式
        checkEmail:function () {
            let regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
            if (!regEmail.test(this.user.email)){
                this.msg="邮箱格式不正确，请重新输入";
            }else{
                axios({
                    url:`common/checkEmail/${this.user.email}`
                }).then(response=>{
                    if (!response.data.flag){
                        this.msg="";
                    }else{
                        this.msg="该邮箱不存在，请重新输入";
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                });
            }
        },
        //发送验证码
        sendCode:function(){
            if (this.msg !== ''){
                layer.msg("请填写正确的邮箱格式");
                return false;
            }
            axios.put(`common/sendCode/${this.user.email}`).then(rsp=>{
                if (rsp.data.flag){
                    layer.msg("发送成功")
                }
            })
        },
        checkPassword:function(){
            if (this.user.password ==="" || this.user.password.length<4 ){
                this.msg = "密码不符合规范，请输入4位以上密码"
            }
        },
        //重置密码
        resetting:function () {
            if (this.msg !== '' ){
                layer.msg(this.msg);
                return false;
            }
            axios.put(`user/updatePassword/${this.code}`,this.user)
                .then(resp=>{
                    if (resp.data.flag){
                        layer.msg("修改成功");
                        // 删除cookie
                        $.removeCookie("userId", {path: '/'});
                        //删除session
                        sessionStorage.removeItem("loginUser");
                    }else{
                        layer.msg(resp.data.data);
                    }
                })
        }
    }
});